package com.htj.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.htj.admin.dao.CartDao;
import com.htj.admin.domain.Cart;
import com.htj.admin.service.CartServiceInf;
import com.htj.util.DateTimeUtil;
import com.htj.vo.PageListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CartServiceImpl implements CartServiceInf {
    @Autowired
    CartDao cardDao;

    @Override
    public boolean add(Cart card) {
        boolean bool = false;
        card.setCreateTime(DateTimeUtil.getSysTime());
        int flag = cardDao.add(card);
        if(flag == 1){
            bool = true;
        }
        return bool;
    }

    @Override
    public PageListVo<Cart> pageList(Map<String, String> map) {
        PageListVo<Cart> pageListVo = new PageListVo<>();
        String currentPage = map.get("currentPage");
        String pageSize = map.get("pageSize");
        PageHelper.startPage(Integer.valueOf(currentPage),Integer.valueOf(pageSize));
        List<Cart> list = cardDao.pageList(map);
        PageInfo<Cart> pageInfo = new PageInfo<>(list);
        List<Cart> pageInfoList = pageInfo.getList();
        int total = (int) pageInfo.getTotal();

        pageListVo.setList(list);
        pageListVo.setTotal(total);
        return pageListVo;
    }

    @Override
    public boolean update(Cart card) {
        boolean bool = false;
        int flag = cardDao.update(card);
        if(flag == 1){
            bool = true;
        }
        return bool;
    }

    @Override
    public boolean delete(Integer id) {
        boolean bool = false;
        int flag = cardDao.delete(id);
        if(flag == 1){
            bool = true;
        }
        return bool;
    }
    @Override
    public boolean deleteByfoodIdAnduserId(Integer userId,Integer foodId) {
        boolean bool = false;
        int flag = cardDao.deleteByfoodIdAnduserId(userId,foodId);
        if(flag == 1){
            bool = true;
        }
        return bool;
    }

    @Override
    public boolean handleChange(Map<String, String> map) {
        boolean bool = false;
        int flag = cardDao.handleChange(map);
        if(flag == 1){
            bool = true;
        }
        return bool;
    }

    @Override
    public Cart getByFoodIdAndUserId(Integer foodId, Integer userId) {
        Cart cart = cardDao.getByFoodIdAndUserId(foodId,userId);
        return cart;
    }

}
